/*
 * eID Test PKI Project.
 * Copyright (C) 2012 Frank Cornelis.
 * Copyright (C) 2012 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package be.fedict.eid.pki.webapp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import be.fedict.eid.pki.entity.CertificateAuthorityEntity;
import be.fedict.eid.pki.model.CertificateAuthorityManagerBean;

@WebServlet(value = "/crls/*")
public class CrlsServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private static final Log LOG = LogFactory.getLog(CrlsServlet.class);

	@EJB
	private CertificateAuthorityManagerBean certificateAuthorityManagerBean;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String requestUri = request.getRequestURI();
		LOG.debug("request uri: " + requestUri);
		if (requestUri.endsWith(".crl")) {
			String caName = FilenameUtils.getBaseName(requestUri);
			LOG.debug("CA name: " + caName);
			printCRL(caName, response);
		} else {
			printIndex(response);
		}
	}

	private void printCRL(String caName, HttpServletResponse response)
			throws IOException {
		response.setContentType("plain/text");
		OutputStream outputStream = response.getOutputStream();
		InputStream crlInputStream = this.certificateAuthorityManagerBean
				.getCRL(caName);
		IOUtils.copy(crlInputStream, outputStream);
	}

	private void printIndex(HttpServletResponse response) throws IOException {
		response.setContentType("text/html");
		PrintWriter printWriter = response.getWriter();
		printWriter.println("<html>");
		{
			printWriter.println("<body>");
			{
				printWriter.println("<table>");
				List<CertificateAuthorityEntity> certificateAuthorities = this.certificateAuthorityManagerBean
						.getCertificateAuthorities();
				for (CertificateAuthorityEntity certificateAuthority : certificateAuthorities) {
					printWriter.println("<tr>");
					{
						printWriter.println("<td>");
						String filename = certificateAuthority.getName()
								+ ".crl";
						printWriter.println("<a href=\"" + filename + "\">"
								+ filename + "</a>");
						printWriter.println("</td>");
					}
					printWriter.println("</tr>");
				}
				printWriter.println("</table>");
			}
			printWriter.println("</body>");
		}
		printWriter.println("</html>");
	}
}
